package gew.system;

import gew.services.DimensionValueService;
import gew.utils.SqlUtils;
import javax.servlet.http.HttpServletRequest;

public class RequestParameterTree extends RequestParameterBase implements IRequestParameterTree {

    String filterCode;
    String filterRequestType;
    String urlTemplate;
    String selectedIdsCS;
    String parentIdCS;
    String filterText;

    public RequestParameterTree(HttpServletRequest request, DimensionValueService dvs) {
        super(request, dvs);
    }

    void initFilterCode(final HttpServletRequest request) {
        if (request.getParameterMap().containsKey(Parameters.FILTER_CODE)) {
            filterCode = request.getParameterMap().get(Parameters.FILTER_CODE)[0];
        } else {
            filterCode = SqlUtils.EMPTY;
        }
    }

    void initFilterRequestType(final HttpServletRequest request) {
        if (request.getParameterMap().containsKey(Parameters.FILTER_REQUEST_TYPE)) {
            filterRequestType = request.getParameterMap().get(Parameters.FILTER_REQUEST_TYPE)[0];
        } else {
            filterRequestType = SqlUtils.EMPTY;
        }
    }

    void initUrlTemplate(final HttpServletRequest request) {
        if (request.getParameterMap().containsKey(Parameters.URL_TEMPLATE)) {
            urlTemplate = request.getParameterMap().get(Parameters.URL_TEMPLATE)[0];
        } else {
            urlTemplate = SqlUtils.EMPTY;
        }
    }

    void initSelectedIdsCS(final HttpServletRequest request, final DimensionValueService dvs) {
        if (request.getParameterMap().containsKey(Parameters.SELECTED_IDS)) {
            selectedIdsCS = request.getParameterMap().get(Parameters.SELECTED_IDS)[0];
        } else {
            selectedIdsCS = SqlUtils.EMPTY;
        }
    }

    private void initParentIdCS(final HttpServletRequest request) {
        if (request.getParameterMap().containsKey(Parameters.PARENT_ID)) {
            parentIdCS = request.getParameterMap().get(Parameters.PARENT_ID)[0];
        } else {
            parentIdCS = SqlUtils.ZERO;
        }
    }

    private void initFilterText(final HttpServletRequest request) {
        if (request.getParameterMap().containsKey(Parameters.FILTER_TEXT)) {
            try {
                filterText = request.getParameterMap().get(Parameters.FILTER_TEXT)[0];
                filterText = filterText
                        .replace("--", SqlUtils.EMPTY)
                        .replace("*", SqlUtils.EMPTY)
                        .replace("'", SqlUtils.EMPTY)
                        .replace("/", SqlUtils.EMPTY)
                        .replace("\\", SqlUtils.EMPTY);
                String[] words = filterText.split(SqlUtils.SPACE);
                filterText = SqlUtils.EMPTY;
                for (int i = 0; i < words.length; i++) {
                    if (words[i].trim().length() > 2) {
                        filterText = filterText.concat(SqlUtils.SPACE).concat(words[i].trim());
                    }
                }
            } catch (NumberFormatException e) {
                filterText = SqlUtils.EMPTY;
            }
        } else {
            filterText = SqlUtils.EMPTY;
        }
    }

    @Override
    void init(final HttpServletRequest request, final DimensionValueService dvs) {
        initFilterCode(request);
        initFilterRequestType(request);
        initUrlTemplate(request);
        initSelectedIdsCS(request, dvs);
        initParentIdCS(request);
        initFilterText(request);
    }

    @Override
    public String getFilterCode() {
        return filterCode;
    }

    @Override
    public String getSelectedIdsCS() {
        return selectedIdsCS;
    }

    @Override
    public String getParentIdCS() {
        return parentIdCS;
    }

    @Override
    public String getFilterText() {
        return filterText;
    }

    @Override
    public String getFilterRequestType() {
        return filterRequestType;
    }

    @Override
    public String getUrlTemplate() {
        return urlTemplate;
    }
}
